package netty.server;

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import netty.model.MethodInvokeMeta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 多线程共享
 *
 * @author Administrator
 */
@Component
@Sharable
public class ServerChannelHandlerAdapter extends ChannelHandlerAdapter
{
	@Autowired
	private RequestDispatcher requestDispatcher;

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
		throws Exception
	{
		MethodInvokeMeta invokeMeta = (MethodInvokeMeta)msg;
		requestDispatcher.dispatcher(ctx, invokeMeta);
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
		throws Exception
	{
		cause.printStackTrace();
		ctx.close();
	}

	public void setDispatcher(RequestDispatcher dispatcher)
	{
		this.requestDispatcher = dispatcher;
	}

}
